package com.liyuxiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyuxiang.constants.SystemConstants;
import com.liyuxiang.mapper.ArticleMapper;
import com.liyuxiang.pojo.entity.Article;
import com.liyuxiang.pojo.entity.Category;
import com.liyuxiang.pojo.vo.CategoryVo;
import com.liyuxiang.service.ArticleService;
import com.liyuxiang.service.CategoryService;
import com.liyuxiang.mapper.CategoryMapper;
import com.liyuxiang.utils.BeanCopyUtils;
import com.liyuxiang.vm.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyuxiang
 * @description 针对表【sg_category(分类表)】的数据库操作Service实现
 * @createDate 2022-03-26 21:10:45
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public List<CategoryVo> getCategoryList() {
        //先查询文章，必须是正式文章，并返回所分类的id   这里目的是：如果分类不是正式的话，那么这个分类下的文章也不应该被查询到
        List<Article> articleList = articleMapper.selectList(new LambdaQueryWrapper<Article>().eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL));
        Set<Long> categoryId = articleList.stream().map(article -> article.getCategoryId()).collect(Collectors.toSet());
        List<CategoryVo> categoryList = BeanCopyUtils.copyBeanList(categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL).in(Category::getId, categoryId)), CategoryVo.class);
//        Assert.notNull(categoryList, "分类列表为空");
        return categoryList;
    }
}




